package cn.liyicun.cp.cpweb.controller.api;

import cn.liyicun.cp.cpweb.base.Result;
import cn.liyicun.cp.cpweb.controller.BaseController;
import cn.liyicun.cp.cpweb.entity.MoneyWithdraw;
import cn.liyicun.cp.cpweb.service.MoneyService;
import cn.liyicun.cp.cpweb.service.money.MoneyTypeEnum;
import cn.liyicun.cp.cpweb.service.money.RechargeService;
import cn.liyicun.cp.cpweb.service.money.WithdrawService;
import cn.liyicun.cp.cpweb.util.CPConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping(value = {CPConstant.ROOT_PHONE + "/money"})
public class MoneyApiController extends BaseController {

    @Autowired
    private MoneyService moneyService;
    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private WithdrawService withdrawService;


    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    public Result<?> recharge(@RequestParam(value = "type", required = true) String type,
                              @RequestParam(value = "remark", required = true) String remark,
                              @RequestParam(value = "rechargeId", required = true) Long rechargeId,
                              @RequestParam(value = "totleMoney", required = true) BigDecimal totleMoney, HttpServletRequest request) {

        Long uid = getLoginById(request);
        int i = rechargeService.recharge(type, remark, rechargeId, totleMoney, uid);

        return Result.success("c充值成功,等待入款!");
    }

    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    public Result<?> withdraw(HttpServletRequest request, @RequestParam(value = "totleMoney", required = true) BigDecimal totleMoney) {
        Long uid = getLoginById(request);

        //TODO  提现先需要检查是否押注一倍流水
        compare(totleMoney.compareTo(BigDecimal.valueOf(0)) <= 0, "必须大于等于0");
        MoneyWithdraw param = new MoneyWithdraw();
        param.setCreateTime(new Date());
        param.setRemark("提现");
        param.setStatus(0);
        param.setTotleMoney(totleMoney);
        param.setUid(uid);
        moneyService.ext(request, MoneyTypeEnum.withdraw.name(), 0L, "yes", "", param);
        return Result.success("发起提现申请成功,请稍等!");
    }

}
